package com.dense.kuiniu.bookborrow.xjSetting.view.other;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.xjSetting.listener.XJOnClickListener;
import com.dense.kuiniu.bookborrow.xjSetting.manager.PingBaoManager;

public class PingBaoLayout extends LinearLayout   {
    private String TAG = "PingBaoLayout";

    public PingBaoLayout(Context context) {
        this(context, null);
    }

    public PingBaoLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PingBaoLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private ImageView pingbao;

    private EditText open_pingbao;
    private EditText cut_pingbao;
    private TextView save;

    private boolean PingBao_open = false;

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.ping_bao_layout, this);

        pingbao = findViewById(R.id.pingbao);

        open_pingbao = findViewById(R.id.open_pingbao);
        cut_pingbao = findViewById(R.id.cut_pingbao);

        save = findViewById(R.id.pingbao_save);
        pingbao.setOnClickListener(onClickListener);
        save.setOnClickListener(onClickListener);

        PingBao_open = PingBaoManager.getInstance().getPingBaoOpen();
        open_pingbao.setText(String.valueOf(PingBaoManager.getInstance().getOpenTime()/1000));
        cut_pingbao.setText(String.valueOf(PingBaoManager.getInstance().getCutTime()/1000));
        setPingbao();
    }


    private void setPingbao() {
        pingbao.setImageResource(PingBao_open ? R.mipmap.open_yuyin : R.mipmap.close_yuyin);
    }



    private XJOnClickListener onClickListener = new XJOnClickListener() {
        @Override
        public void onclickView(View view) {
            switch (view.getId()) {
                case R.id.pingbao:
                    PingBao_open = !PingBao_open;
                    setPingbao();
                    break;
                case R.id.pingbao_save:
                    String open = open_pingbao.getText().toString();
                    String cut = cut_pingbao.getText().toString();
                    if (StringUtils.isEmpty(open) || StringUtils.isEmpty(cut)) {
                        ToastUtils.showShort("输入不能为空");
                        return;
                    }
                    try {
                        int opv = Integer.parseInt(open);
                        if (opv >= 1 && opv <= 1000) {
                            PingBaoManager.getInstance().setOpenTime(opv*1000);
                        } else {
                            ToastUtils.showShort("开启屏保时间，请输入1-1000之间的数字");
                            return;
                        }
                    } catch (Exception e) {
                        ToastUtils.showShort("时间只能是整数！！");
                        return;
                    }
                    try {
                        int cutV = Integer.parseInt(cut);
                        if (cutV >= 1 && cutV <= 1000) {
                            PingBaoManager.getInstance().setCutTime(cutV*1000);
                        } else {
                            ToastUtils.showShort("界面切换时间，请输入1-1000之间的数字");
                            return;
                        }
                    } catch (Exception e) {
                        ToastUtils.showShort("时间只能是整数！！");
                        return;
                    }


                    PingBaoManager.getInstance().setPingBaoOpen(PingBao_open);
                    ToastUtils.showShort("保存成功");
                    break;
            }
        }
    };
}

